//
//  RadioDetailsViewController.swift
//  FM
//
//  Created by x on 2017/10/31.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class RadioDetailsViewController: UIViewController {

    /// 当前电台
    var currentRadio:Radio?
    
    var index:Int = 1
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        navigationController?.navigationBar.isHidden = true
     }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        navigationController?.navigationBar.isHidden = false
    }
    
    /// 初始化
    private func setupUI() -> Void {
        view.backgroundColor = UIColor(patternImage: UIImage.init(named: "BackImagreView")!)
        //取消系统导航栏昂我们设置的UIScrollerView的contenInsets属性
        automaticallyAdjustsScrollViewInsets = false
        
        //1.添加子控件
        
        view.addSubview(bgToolbar)
        view.addSubview(tableView)
        
        let btu = UIButton.buttonWith(image: "more222", hightImage: "", tag: 1, target: self, action: #selector(leftBtuClick))
        btu.translatesAutoresizingMaskIntoConstraints = true
        btu.frame = CGRect(x: 10, y: 30, width: 30, height: 30)
        view.addSubview(btu)
        
        
    
        //2.初始化数据
        loadData(offset: 0)
    }
    
    func leftBtuClick() -> Void {
        navigationController?.popViewController(animated: true)
    }
    
    /// 加载数据
    ///
    /// - Parameter offset: 请求数据的下表
    func loadData(offset:Int) -> Void {
        NetWorTools.shareNetWorkTools().loadRadioDetails(diantai_id: (currentRadio?.id)!, offset: offset) { (data, error) in
            if error == nil {
                for fm in data! {
                    fm.diantai = self.currentRadio
                }
                self.FMlist += data!
                self.tableView.reloadData()
            }else {
                print("loadRadioDetails======",error ?? 0)
            }
        }
    }
    
    /// 下拉刷新
    func loadMoreData() -> Void {
        index += 1
        let num = index * 20
        
        loadData(offset: num)
    }
    
    private lazy var bgToolbar:UIToolbar = {
        let toolBar = UIToolbar()
        toolBar.frame = self.view.bounds
        toolBar.barStyle = UIBarStyle.blackOpaque
        
        return toolBar
    }()
    
    fileprivate lazy var tableView:UITableView = {
        let tableView = UITableView();
        tableView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.clear
        
        let headView = RadioHeadView()
        headView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: headView.headViewHeight(model: self.currentRadio!))
        
        tableView.tableHeaderView = headView;
        
        tableView.register(CategoryTableViewCell.self, forCellReuseIdentifier: radioDetailsCellIdentifier)
        tableView.mj_footer = MJRefreshBackNormalFooter(refreshingTarget: self, refreshingAction: #selector(loadMoreData))
        
        return tableView;
    }()
    
    fileprivate lazy var FMlist:[HomeFM] = [HomeFM]()
    
    fileprivate lazy var cachesHeight:[String:CGFloat] = [String:CGFloat]()
}

private let radioDetailsCellIdentifier = "radioDetailsCellIdentifier"

extension RadioDetailsViewController:UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return FMlist.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: radioDetailsCellIdentifier) as! CategoryTableViewCell
        
        cell.model = FMlist[indexPath.row]
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        //增加一个专场动画
        AnimationTools.addAnimationToKeyWindow(animationType: "rippleEffect")
        
        //跳转VC
        let playController = HomePlayerViewController(data: FMlist, index: indexPath.row)
        
        navigationController?.pushViewController(playController, animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        let FM = FMlist[indexPath.row]
        let height = cachesHeight[FM.id!]
        if height != nil{
            return height!
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: radioDetailsCellIdentifier) as! CategoryTableViewCell
        
        let rowHeight = cell.rowHeight(model: FM)
        
        cachesHeight[FM.id!] = rowHeight
        
        return rowHeight
    }
}









